<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

//数组优良展示的打印方法
if (!function_exists('p')) {
    function p($data)
    {
        echo "<pre>";
        print_r($data);
        echo "</pre>";
    }
}

//json优良展示的打印方法
if (!function_exists('pjson')) {
    function pjson($data)
    {
        header("Content-type: application/json; charset=utf-8", true);
        echo json_encode($data, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
    }
}


//统一输出格式话的json数据
if (!function_exists('out')) {
    function out($data = '', $code = '200', $msg = 'success')
    {
        $out = array('code' => $code, 'msg' => $msg, 'data' => $data);
        return json($out);
    }
}

//生成唯一的token字符串
if (!function_exists('generate_token')) {
    function generate_token($type = 'md5')
    {
        if ($type == 'sha1'){
            $token = sha1(uniqid().rand(-100000, 100000));
        }
        else {
            $token = md5(uniqid().rand(-100000, 100000));
        }
        return $token;
    }
}

/*
 * 公钥加密
 */
if (!function_exists('public_key_encrypt')) {
    function public_key_encrypt($data)
    {
        $public_key = config('rsa_public_key');
        $pu_key = openssl_pkey_get_public($public_key);
        openssl_public_encrypt($data, $encryptData, $pu_key);
        $encryptData = base64_encode($encryptData);
        return $encryptData;
    }
}

/*
 * 私钥解密
 * 当密文错误时，返回的是nul, 正确时就是返回的解析出来的明文，一般规定的交互格式是json
 */
if (!function_exists('private_key_decrypt')) {
    function private_key_decrypt($encryptData)
    {
        $private_key = config('rsa_private_key');
        $pi_key = openssl_pkey_get_private($private_key);
        $encryptData = base64_decode($encryptData);
        openssl_private_decrypt($encryptData, $data, $pi_key);
        return $data;
    }
}

/*
 * 保存文件
 */
if (!function_exists('save_file')) {
    function save_file($filepath, $filecontent)
    {
        $local_file = fopen($filepath, 'w');
        if(false !== $local_file) {
            if (false !== fwrite($local_file, $filecontent)) {
                fclose($local_file);
                return true;
            }
        }

        return false;
    }
}

//PHP stdClass Object转array
function object_array($array)
{
    if (is_object($array)) {
        $array = (array) $array;
    }if (is_array($array)) {
        foreach ($array as $key => $value) {
            $array[$key] = object_array($value);
        }
    }
    return $array;
}

/**
 * [sortMembers description]
 * @param  [type] $memberData [description]
 * @return [type]             [description]
 */
function sortMembers($str,$data = array())
{
    $key_arr = array_column($data,$str);
    array_multisort($key_arr,SORT_DESC,$data);
    return $data;
}

/**
 * @param bool $success
 * @param string $message
 * @param null $data
 * @return array
 */
function msgReturn($success=TRUE, $message='success',$data=NULL){
    $resp = [];
    $resp['message'] = $message;
    $resp['code'] = $success ? 200 : 100;
    if ($success){
        $resp['data'] = $data;
    }
    return $resp;
}